package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.FinanceService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class FinanceServiceImpl implements FinanceService {
    @Autowired
    public FinanceDao financeDao;

    @Autowired
    private PackingDao packingDao;
    @Autowired
    private ExportDao exportDao;
    @Override
    public void save(Finance finance) {
        finance.setState("0");
        financeDao.insertSelective(finance);
        Packing packing = packingDao.selectByPrimaryKey(finance.getFinanceId());
        if (packing!=null){
            String exportIds = packing.getExportIds();
            String[] ids = exportIds.split(",");
            for (String id : ids) {
                Export export = new Export();
                export.setId(id);
                export.setState(5);
                exportDao.updateByPrimaryKeySelective(export);
            }
        }

    }

    @Override
    public void update(Finance finance) {
        financeDao.updateByPrimaryKeySelective(finance);
    }

    @Override
    public void delete(String financeId) {
        financeDao.deleteByPrimaryKey(financeId);
    }

    @Override
    public Finance findById(String financeId) {
        return financeDao.selectByPrimaryKey(financeId);
    }

    @Override
    public PageInfo findAll(FinanceExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<Finance> finances = financeDao.selectByExample(example);
        return new PageInfo(finances);
    }

}
